<%@page pageEncoding="utf-8" contentType="text/html; charset=utf-8" %>
<%@ include file="/admin/include/init_admin.jsp" %>
<%@ page import="com.welfare.admin.call.*" %>
<%

	HashList code308		= CommonUtil.getCodeList("308");	//상용문구
	
	String sms_id 			= StringUtil.nvl(request.getParameter("sms_id"));
	String customer_id		= StringUtil.nvl(request.getParameter("customer_id"));
	String customer_name	= StringUtil.nvl(request.getParameter("customer_name"));
	String hp_no			= StringUtil.nvl(request.getParameter("hp_no"));

	boolean isSend			= "".equals(sms_id) ? true : false; 

	HashMap infos	= new HashMap();
	infos.put("sms_id", sms_id);
	
	CallDAO dao 	= new CallDAO();
	HashList info 	= dao.selectSmsInfo(infos);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<meta http-equiv="X-UA-Compatible" content="IE=Edge"/>	<!-- 팝업은 자동으로 호환성보기로 오픈되어서, 강제로 설정 -->
	
	<title><%= SYSTEM_NAME %></title>
	<%@ include file="/admin/include/cssnjs.jsp"  %>
	
	<script type="text/javascript" src="../../js/sms.js"></script>

	<script type="text/javascript">
	
	
	
	$(document).ready(function() {
		$('input[name="subject"]').focus();
		
		$('#sms_list').find('tr').hover(
				function () { $(this).addClass("hover"); }
			  , function () { $(this).removeClass("hover"); }
			)
			.click(function() {
				document.frm.subject.value = $(this).find('.subject').text();
				document.frm.sms_msg.value = $(this).find('.sms_msg').html().replace(/<br>/g, '\r\n').replace(/<BR>/g, '\r\n');
				checkMsg();
			});
	});

	//SMS 발송
	function fnSendSms() {
		if (cfCheckRequired(document.frm.subject, '제목을 입력하세요')
				&& cfCheckRequired(document.frm.dest_no, '받는사람 휴대전호번호를 입력하세요')
				&& cfCheckRequired(document.frm.callback_no, '회신번호를 입력하세요')
				&& cfCheckRequired(document.frm.sms_msg, '메세지를 입력하세요')) 
		{
			if (confirm(MSG_SEND)) 
			{
				var sms_gubn = "sms";
				if (document.getElementById("msgLen").innerText > 80) {
					sms_gubn = "mms";	
				} 
				
				document.frm.sms_gubn.value = sms_gubn;
				document.frm.dest_info.value = nvl(document.frm.dest_name.value,"이름없음") + "^" + document.frm.dest_no.value;
				
				$.ajax({
					url: './smsSend.do',
					type: "POST",
				    dataType: 'json',//html,text,json
				    data: $('#frm').serialize(),
				    error: function(data, status, err){
				    	alert(err);
				    },
					success: function(data){
						alert(sms_gubn.toUpperCase() + "가 발송되었습니다.");
						fnReset();
				    }
				});
			}
		}
		
		return false;
	}

	//입력폼 초기화
	function fnReset() {
		document.getElementById("msgLen").innerText = "0";
		document.frm.reset(); 
		document.frm.subject.focus();
	}
	</script>
</head>
<body>
<div id="wrap">
		<div id="popup">
		
			
			<div class="menu_title">
				<h1>문자발송</h1>
				<div class="r mt2">
				<%	if (!isSend) { %>
					<input type="image" class="button" src="../images/btn_close.gif" onclick="self.close()"/>
				<%	} %>
				</div>
			</div>
			<div class="clear line_01"></div>


			<form name="frm" id="frm" method="post" onsubmit="return fnSendSms()">
			<input type="hidden" name="customer_id" value="<%= customer_id %>" />
			<input type="hidden" name="sms_gubn" value="sms"/>
			
			<table cellpadding="0" cellspacing="0" class="table_01 mt5">
			<colgroup>
				<col width="80px"/>
				<col width="210px"/>
				<col width="80px"/>
				<col />
			</colgroup>
			
			<tbody>
				<tr>
					<th><p>제목</p></th>
					<td colspan="3">
					<%	if (isSend) { %>
						<input type="text" name="subject" value="" class="text wmax han"/>
					<%	} else { %>
						<%= info.get("subject",0) %>
					<%	} %>
					</td>
				</tr>
				<tr>
					<th><p>받는사람</p></th>
					<td>
					<%	if (isSend) { %>
						<input type="text" name="dest_info" class="hidden" value=""/>
						<input type="text" name="dest_name" class="text w60" value="<%= customer_name %>" placeholder="이름"/>
						<input type="text" name="dest_no" class="text w100" value="<%= hp_no %>" placeholder="휴대전화번호"/>
					<%	} else { %>
						<%= info.get("dest_info",0) %>
					<%	} %>
					</td>
					<th><p>회신번호</p></th>
					<td>
					<%	if (isSend) { %>
						<input type="text" name="callback_no" class="text" value="023301004"/></td>
					<%	} else { %>
						<%= info.get("callback_no",0) %>
					<%	} %>
				</tr>
				<tr>
					<th><p>메세지</p></th>
					<td <%= isSend ? "" : "colspan=3" %>>
					<%	if (isSend) { %>
						<div id="sms">
							<textarea name="sms_msg" id="sms_msg"  onkeyup="checkMsg()" ></textarea>
							<p><label id="msgLen">0</label> / 2000 byte</p>
						</div>
					<%	} else { %>
						<p style="min-height:140px">
						<%= info.getToBr("sms_msg",0) %>
						</p>
					<%	} %>
					</td>
				<%	if (isSend) { %>
					<th><p>상용문구</p></th>
					<td>
						<table width="100%" cellpadding="0" cellspacing="0" style="border-right:1px solid #b7b7b7">
						<colgroup>
							<col width="30px"/>
							<col />
						</colgroup>
						<thead>
							<tr>
								<th><p>No</p></th>
								<th class="brx"><p>제목</p></th>
							</tr>
						</thead>
						</table>
						
						
						<div class="box" style="height:250px;">
						<table width="100%" cellpadding="0" cellspacing="0" style="border-left:0;">
						<colgroup>
							<col width="29px"/>
							<col />
						</colgroup>
						<tbody id="sms_list">
					<%	for (int i = 0; i < code308.size(); i++) { %>
							<tr>
								<td class="blx c"><%=i+1 %></td>
								<td class="brx">
									<span class="subject"><%= code308.get("code_name",i) %></span>
									<span class="sms_msg hidden"><%= code308.get("code_desc",i) %></span>
								</td>
							</tr>
					<%	} %>
						</tbody>
						</table>
						</div>
					
					</td>
				<%} %>
				</tr>
				<tr>
					<th><p>발송일시</p></th>
					<td><%= info.getDate("send_date",0) %></td>
					<th><p>발송결과</p></th>
					<td><%= info.getToBr("success_yn",0) %></td>
				</tr>
			</tbody>
			</table>
			
			<%	if (isSend) { %>
			<div class="mt10 c">
				<input type="image" class="button" src="../images/btn_send.gif"/>
				<a href="#nohref" onclick="fnReset()"><img src="../images/btn_init.gif"/></a>
				<a href="https://smvf.sdm.go.kr/login.do?cmd=list&user_id=1004call&user_pwd=1004call" target="_blank"><img src="../images/btn_send_group.gif"/></a>
				<a href="#nohref" onclick="self.close()"><img src="../images/btn_close.gif"/></a>
			</div>
			<%	} %>
			</form>
		</div>
		
</div>



</body>
</html>
